#include "stack.h"

struct Stack* createStack(int capacity, 
						  void (*_cpData)(void*, const void*),
						  void (*_printInfo)(void*),
						  int (*_dataSize)(),
						  void* (*_getPointer)(void*, int))
{
	assert(capacity > 0);

	
	struct Stack* pstack =(struct Stack*)calloc(sizeof(struct Stack), 1);
	if(pstack==NULL)
		return NULL;
	pstack->_cpData = _cpData;
	pstack->_printInfo = _printInfo;
	pstack->_dataSize = _dataSize;
	pstack->_getPointer = _getPointer;


	pstack->base = calloc(pstack->_dataSize(), capacity);
	
	if(pstack->base==NULL)
	{
		free(pstack);
		return NULL;
	}

	pstack->stack_size = capacity;
	pstack->max_stack = capacity - 1;
	pstack->min_stack = 0;

	clearStack(pstack);
	return pstack;
}


void clearStack(struct Stack * _pstack)
{
	_pstack->top = -1;
}

void deleteStack(struct Stack* _pstack)
{
	if(_pstack==NULL)
		return;

	free(_pstack->base);
	free(_pstack);
}

int popElement(struct Stack* _pstack, void * buf)
{
	if(_pstack->top==-1)
		return 0;

	_pstack->_cpData(buf, _pstack->_getPointer(_pstack->base, _pstack->top--));
	return 1;
}

int pushElement(struct Stack* _pstack, const void * to_push)
{
	if(_pstack->top==_pstack->max_stack)
		return 0;	
	_pstack->_cpData(_pstack->_getPointer(_pstack->base, ++_pstack->top), to_push);

	return 1;
}

void printInfo(struct Stack* _pstack)
{
	int i = 0;
	for(;i<=_pstack->top;i++)
		_pstack->_printInfo(_pstack->_getPointer(_pstack->base, i));
	
	return;
}

